package com.jiaquyun.jcyx.jetpack.mode

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.jiaquyun.jcyx.base.BaseViewModel
import com.jiaquyun.jcyx.entity.*
import com.jiaquyun.jcyx.jetpack.api.ArticleRepository
import com.jiaquyun.jcyx.jetpack.api.NotifyRepository
import com.jiaquyun.jcyx.jetpack.api.OrderRepository
import com.jiaquyun.jcyx.jetpack.api.UserRepository
import com.jiaquyun.jcyx.main.cart.CartSettlementActivity
import com.module.lemlin.http.HttpResponseBody
import com.module.lemlin.http.HttpResponseData
import com.module.lemlin.http.SubscribeViewModel

class OrderViewModel(application: Application) : BaseViewModel(application) {

    private val mOrderRepository = OrderRepository()

    val orderSubmitLiveData = MutableLiveData<HttpResponseBody<OrderSubmitResponse>>()
    fun orderSubmit(
        orderData: OrderData
    ) {
        setRxLifeScope(SubscribeViewModel(orderSubmitLiveData)) {
            mOrderRepository.orderSubmit(orderData)
        }
    }

    var page: Int = 1
    var rows:Int = 20

    val orderListLiveDate = MutableLiveData<HttpResponseBody<OrderListResponse>>()
    fun orderIndex(type:String,isLoadMore: Boolean = false) {
        val listener = object : SubscribeViewModel<OrderListResponse>(orderListLiveDate) {
            override fun onSuccess(resp: OrderListResponse) {
                super.onSuccess(resp.apply { loadMore = page > 1 })
                page += 1
            }
        }
        if (!isLoadMore)
            page = 1
        setRxLifeScope(listener) {
            mOrderRepository.orderIndex( type,page.toString(), rows.toString())
        }
    }

    val orderDetailsLiveData = MutableLiveData<HttpResponseBody<OOrderDetailsResponse>>()
    fun orderDetail(orderId: String) {
        setRxLifeScope(SubscribeViewModel(orderDetailsLiveData)) {
            mOrderRepository.orderDetail(orderId)
        }
    }

    val orderPayLiveData = MutableLiveData<HttpResponseBody<OrderPayResponse>>()
    fun orderPay(orderId: String, payType: String) {
        setRxLifeScope(SubscribeViewModel(orderPayLiveData)) {
            mOrderRepository.orderPay(orderId,payType,"app")
        }
    }

    val orderCancelLiveData = MutableLiveData<HttpResponseBody<HttpResponseData>>()
    fun orderCancel(orderId: String) {
        setRxLifeScope(SubscribeViewModel(orderCancelLiveData)) {
            mOrderRepository.orderCancel(orderId)
        }
    }

    val orderConfirmLiveData = MutableLiveData<HttpResponseBody<HttpResponseData>>()
    fun orderConfirm(orderId: String) {
        setRxLifeScope(SubscribeViewModel(orderConfirmLiveData)) {
            mOrderRepository.orderConfirm(orderId)
        }
    }
}